from django.contrib.auth.decorators import login_required
from django import oldforms
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponse, HttpResponseRedirect
from django.template import Context, loader
from django.shortcuts import render_to_response, get_object_or_404

# models
from rcbn.characters.models import Character
from django.contrib.auth.models import User

# forms
from django import newforms as forms

def index(request):
    u = request.user
    t = loader.get_template('index.html')
    is_logged_in  = u.is_authenticated()
    c = Context({'user': u, 'is_logged_in': is_logged_in})
    return HttpResponse(t.render(c))

@login_required
def profile(request):
    u = request.user
    t = loader.get_template('profile.html')
    is_logged_in  = u.is_authenticated()
    c = Context({'user': u, 'is_logged_in': is_logged_in})
    return HttpResponse(t.render(c))

def register(request):
    form = UserCreationForm()
    
    if request.method == 'POST':
        data = request.POST.copy()
        errors = form.get_validation_errors(data)
        if not errors:
            new_user = form.save(data)
            return HttpResponseRedirect("/accounts/created")
    else:
        data, errors = {}, {}
        
    return render_to_response("register.html", {'form' : oldforms.FormWrapper(form, data, errors)})

def created(request):
    pass